package com.iasku.fragment;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;

import com.iasku.adapter.CourseAdapter;
import com.iasku.constant.Constant;
import com.iasku.entity.IaskuCourse;
import com.iasku.iaskueleclass_phone.R;
import com.iasku.iaskueleclass_phone.ReturnData;
import com.iasku.iaskueleclass_phone.Status;
import com.iasku.manager.DataManager;
import com.iasku.manager.IDataManager;
import com.iasku.manager.IaskuManager;

public class CourseFragment extends BaseFragment {
    private static final int TASK_GET_COURSE = 1;
    private ListView mCourseLv;
    private List<IaskuCourse> mCourseList;
    
    private void initView(View view){
        mCourseLv = (ListView) view.findViewById(R.id.course_listview);
        addListener();
        startTask(TASK_GET_COURSE);
    }
    
    private void addListener(){
        mBarSearchBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startTask(TASK_GET_COURSE);
            }
        });
       
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.course_fragment_layout,null);
        initTitleBar(R.string.course_title);
        initView(mRootView);
        return mRootView;
    }
    
    
    @Override
    public int onTaskStart(int taskId, Bundle bundle) {
        if(taskId == TASK_GET_COURSE){
            mProgressDialog.setMessage("正在加载...");
            mProgressDialog.show();
            mCourseLv.setVisibility(View.INVISIBLE);
        }
        return super.onTaskStart(taskId, bundle);
    }
    
    private String getSubjectname(){
        switch (IaskuManager.getInstance().getSubjectId()) {
        case 0:
            return "";
        default:
           return IaskuManager.getInstance().getSujectName();
        }
    }
    
    @Override
    public Status onTaskInBackground(int taskId, Bundle bundle) {
        // TODO Auto-generated method stub
        switch (taskId) {
        case TASK_GET_COURSE:{
            IDataManager manager = DataManager.getInstance();
            initTimeText();
           
            ReturnData<List<IaskuCourse>> mReturnData = manager.getCourses(Constant.USER_IASKUNAME,
                        getSubjectname(),time_start_text,time_end_text);
            mCourseList = mReturnData.getData();
            return new Status(mReturnData);
        }
        }
        return super.onTaskInBackground(taskId, bundle);
    }
    
    @Override
    public int onTaskComplete(int taskId, Status status) {
        switch (taskId) {
        case TASK_GET_COURSE:{
            mProgressDialog.dismiss();
            if(mCourseList != null && !mCourseList.isEmpty()){
                mCourseLv.setVisibility(View.VISIBLE);
                CourseAdapter adapter = new CourseAdapter(getActivity().getApplicationContext(), mCourseList);
                mCourseLv.setAdapter(adapter);
            }
        }
            break;

        default:
            break;
        }
        return super.onTaskComplete(taskId, status);
    }
}
